import 'package:dio/dio.dart';
import 'package:logger/logger.dart';

class BtResDataConverterInterceptor extends Interceptor {
  final logger = Logger();

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    if (response.data is Map) {
      try {
        final Map<String, dynamic> newJsonData = <String, dynamic>{};
        newJsonData['code'] = response.data['code'];
        newJsonData['msg'] = response.data['msg'];
        newJsonData['key'] = response.data['key'];
        newJsonData['success'] = response.data['success'];
        newJsonData['fail'] = response.data['fail'];
        newJsonData['data'] = response.data['data'];
        if (response.data['data'] is String) {
          newJsonData['data'] = <String, dynamic>{'value': response.data['data']};
        }
        response.data = newJsonData;
      } catch (e) {
        logger.i('JSON decoding error: $e');
      }
    }
    super.onResponse(response, handler);
  }
}
